package com.woniuxy.consumer;

import com.woniuxy.daos.TbOrderDao;
import com.woniuxy.daos.TbSendmessageDao;
import com.woniuxy.entitys.TbOrder;
import com.woniuxy.entitys.TbSendmessage;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * 消费者
 */
@Component
@RabbitListener(queues = "order_queue")
public class OrderConsumer {

    @Autowired
    private TbOrderDao tbOrderDao;
    @Autowired
    private TbSendmessageDao tbSendmessageDao;

    /**
     * 接收消息
     * @param map
     */
    @RabbitHandler
    public void receive(Map<String, Integer> map) {
        //先去查订单编号对应的订单状态
        TbOrder tbOrder = tbOrderDao.selectByPrimaryKey(map.get("oid"));
        if (tbOrder.getOrderstatus() == 1) {//状态为待付款
            TbSendmessage tbSendmessage = new TbSendmessage();
            tbSendmessage.setCid(map.get("cid"));
            tbSendmessage.setSendTime(new Date());
            tbSendmessage.setMid(2);
            tbSendmessageDao.insert(tbSendmessage);
        }
    }
}
